package com.hrt.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class OcrTools {


	    private final String LANG_OPTION = " -l";  //英文字母小写l，并非数字1  

	    private final String EOL = System.getProperty("line.separator");  

	    private String tessPath ;

	    //private String tessPath = new File("tesseract").getAbsolutePath();  

	      

	    public String recognizeText(File imageFile,String imageFormat)throws Exception{  

//	        File tempImage = ImageIOHelper.createImage(imageFile,imageFormat);  

	        File outputFile = new File(imageFile.getParentFile(),"output");  

	        StringBuffer cmd = new StringBuffer();  
	        StringBuffer strB = new StringBuffer();  

	        cmd.append(tessPath+"/tesseract.exe ");
	        
	        cmd.append(" ");  

	        cmd.append(outputFile.getName());  
	       
	        cmd.append(LANG_OPTION);  
	        cmd.append(" ");  
	        cmd.append("chi_sim"); 
	        //List<String> cmd = new ArrayList<String>();  

	       
	          //  cmd.add(tessPath+"//tesseract");  
	        System.out.println(cmd.toString());
	            ProcessBuilder pb = new ProcessBuilder();  
	            pb.command(cmd.toString());
	            pb.redirectErrorStream(true);  
	            Process process = pb.start();  
//	        cmd.add("");  
//
//	        cmd.add(outputFile.getName());  
//
//	        cmd.add(LANG_OPTION);  
//
//	        cmd.add("chi_sim");  
//
//	        //cmd.add("eng");  
//
//	          
//
//	        ProcessBuilder pb = new ProcessBuilder();  
//
//	        pb.directory(imageFile.getParentFile());  
//
//	          
//
//	        cmd.set(1, imageFile.getAbsolutePath());  
//
//	        pb.command(cmd);  

	//        pb.redirectErrorStream(true);  

	          

	       
	       
	        String s;
	        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
	        while((s=bufferedReader.readLine()) != null);
	        //删除临时正在工作文件  
	        int w = process.waitFor();  
	     

	          

	        if(w==0){  

	            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath()+".txt"),"UTF-8"));  

	              

	            String str;  

	            while((str = in.readLine())!=null){  

	                strB.append(str).append(EOL);  

	            }  

	            in.close();  

	        }else{  

	            String msg;  

	            switch(w){  

	                case 1:  

	                    msg = "Errors accessing files.There may be spaces in your image's filename.";  

	                    break;  

	                case 29:  

	                    msg = "Cannot recongnize the image or its selected region.";  

	                    break;  

	                case 31:  

	                    msg = "Unsupported image format.";  

	                    break;  

	                default:  

	                    msg = "Errors occurred.";  

	            }  

	           

	            throw new RuntimeException(msg);  

	        }  

	        new File(outputFile.getAbsolutePath()+".txt").delete();  

	        return strB.toString();  

	    }

		public String getLANG_OPTION() {
			return LANG_OPTION;
		}

		public String getEOL() {
			return EOL;
		}

		public String getTessPath() {
			return tessPath;
		}

		public void setTessPath(String tessPath) {
			this.tessPath = tessPath;
		}  

}
